#ifndef DIALOGMAIN_H
#define DIALOGMAIN_H

#include <QDialog>
#include <QTcpSocket>
#include "action.h"
#include "layout.h"
#include "actionsettingsdialog.h"

struct SocketServer {
    QString mAddress;
    QString mPortNum;
};

struct UserInfo {
    QString mUserId;
    QString mPassword;
};

#define DEF_INI_FILENAME    "cuberemote.ini"

namespace Ui {
class DialogMain;
}

class DialogMain : public QDialog
{
    Q_OBJECT

public:
    explicit DialogMain(QWidget *parent = 0);
    ~DialogMain();

protected:
    virtual void timerEvent(QTimerEvent *e);

private:
    virtual bool winEvent(MSG *message, long *result);

private:
    void loadConfig(const QString fileName);
    void saveConfig(const QString fileName);
    void initContextMenu();
    void initWidget();
    void initTcpSocket();

    void initLayout();
    void updateStatus(int cid, int bid, int rid, int status);

Q_SIGNALS:
    void signalNeedToShow();

public Q_SLOTS:
    void onParseArgs(const QString& args);
    void onShowContextMenu(const QPoint& pos);
    void onMenuRelayOff();
    void onMenuRelayOn();
    void onMenuRelayAir();
    void onMenuRelayCooling();
    void onMenuRelayHeating();
    void onMenuSocketServer();
    void onMenuSync();
    void onMenuReboot();
    void onMenuActionSetting();
    void onMenuOptionCooling();
    void onMenuOptionHeating();
    void onMenuExit();
    void onButtonClicked();
    void onActionSettingsDialogShow(bool show);

    void onTcpConnect();
    void onTcpConnected();
    void onTcpRead();
    void onTcpConnectionClosedByServer();
    void onTcpError(QAbstractSocket::SocketError socketError);

private:
    Ui::DialogMain *ui;

    int mOneSecondTimerId;

    SocketServer mSocketServer;
    UserInfo mUserInfo;
    QTcpSocket *mpTcpSoceket;
    bool mIsSocketInit;
    QString mTcpSoceketRecv;

    Layout mLayout;
    Action mAction;

    ActionSettingsDialog *mActionSettingsDialog;
};

#endif // DIALOGMAIN_H
